package org.eclipse.ea.requisit.manager.popup.model;

import java.math.BigDecimal;

import org.eclipse.ea.requisit.manager.utils.StringUtils;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

public class MouseDownListener implements Listener {

	private CostsTable table;

	public MouseDownListener(CostsTable table) {

		this.table = table;
	}

	@Override
	public void handleEvent(Event event) {

		Rectangle clientArea = table.getTable().getClientArea();
		Point pt = new Point(event.x, event.y);
		int index = table.getTable().getTopIndex();

		while (index < table.getTable().getItemCount()) {

			boolean visible = false;
			final TableItem item = table.getTable().getItem(index);
			
			final int row = index; 

			for (int i = 0; i < table.getTable().getColumnCount(); i++) {

				Rectangle rect = item.getBounds(i);

				if (rect.contains(pt)) {

					final int column = i;
					final Text text = new Text(table.getTable(), SWT.NONE);
					Listener textListener = new Listener() {

						public void handleEvent(final Event e) {

							switch (e.type) {
							case SWT.FocusOut:
								
								double value = Double.parseDouble(StringUtils.numericOrZero(text.getText()));
								
								if(value < 0 || value > 9){
									value = 0;
								}
								
								item.setText(column, String.valueOf(roundValue(value, 2)));
								text.dispose();
								
								TableItem invetItem = table.getTable().getItem(column - 1);
								
								if((column -  1) != row){
									
									if (value <= 0) {
										invetItem.setText(row + 1, String.valueOf(roundValue(0, 2)));
									} else {
										invetItem.setText(row + 1, String.valueOf(roundValue(1 / value, 2)));
									}
								} else {
									item.setText(column, "-");
								}
								
								
								break;
								
							case SWT.Traverse:
								
								switch (e.detail) {
								
								case SWT.TRAVERSE_RETURN:
									item.setText(column, text.getText());
									// FALL THROUGH
								case SWT.TRAVERSE_ESCAPE:
									text.dispose();
									e.doit = false;
								}

								break;
							}
						}
					};

					text.addListener(SWT.FocusOut, textListener);
					text.addListener(SWT.Traverse, textListener);
					table.getTableEditor().setEditor(text, item, i);
					text.setText(item.getText(i));
					text.selectAll();
					text.setFocus();
					return;
				}

				if (!visible && rect.intersects(clientArea)) {
					visible = true;
				}
			}

			if (!visible) {
				return;
			}

			index++;
		}
	}
	
	private Double roundValue(double value, int decimalPlace){
		
		BigDecimal newValue = new BigDecimal(value);
		newValue = newValue.setScale(decimalPlace, BigDecimal.ROUND_FLOOR);
		return Double.valueOf(newValue.doubleValue());
		
	}
}
